﻿/**************************************************************************
创建时间:	2020/5/14
作	  者:	张存
邮 	  箱:	zhangcunliang@126.com

Copyright (c) zhcun.cn

描	述：
记	录：
***************************************************************************/
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using ZhCun.DbCore;
using ZhCun.DbCore.BuildSQLText;
using ZhCun.DbCore.Cores;
using ZhCun.DBModel;
using ZhCun.Utils;
using ZhCun.Utils.Helpers;
using ZhCun.Win.DB;

namespace ZhCun.Win.ExtendGridCombox
{
    class GridComboxBLL: BLLBase
    {
        /// <summary>
        /// 获取配置
        /// </summary>
        public ApiResult<RetGridCombox> GetConfig(string formName)
        {
            RetGridCombox ret = new RetGridCombox();
            ret.MainConfig = DB.Query<Sys_GridCombox>(s => s.FormName == formName).ToList();
            if (ret.MainConfig == null || ret.MainConfig.Count == 0)
            {
                return ApiResult.RetErr<RetGridCombox>("配置项为空");
            }
            var mainIds = ret.MainConfig.Select(s => s.Id).ToArray();
            ret.ColumnConfig = DB.Query<Sys_GridCombox_Column>(
                s =>
                s.WEx_In(s.MainId, mainIds)
                ).ToList();
            return ApiResult.RetOK(ret);
        }

        #region 表格维护

        /// <summary>
        /// 固定的form名称，用于当前页面维护
        /// </summary>
        public string FixedFormName { set; get; }

        public DataTable GetMainData(int pageNo, int pageSize, string searchVal,out int rowCount)
        {
            var query = DB.CreateQuery<Sys_GridCombox>();
            query.PageNo = pageNo;
            query.PageSize = pageSize;
            if (!FixedFormName.IsEmpty())
            {
                query.WhereAnd(s => s.FormName == FixedFormName);
            }
            if (!searchVal.IsEmpty())
            {
                string likeStr = $"%{searchVal}%";
                query.WhereAnd(s =>
                    s.WEx_Like(s.FormName, likeStr));
            }
            
            query.OrderBy(s => s.FormName).OrderBy(s => s.TextBoxName);
            var ret = DB.Query<Sys_GridCombox>(query, ADJoinSql);
            var dt = ret.ToDataTable();
            rowCount = query.Total;
            return dt;
        }

        ApiResult<string> VerifyMainModel(Sys_GridCombox model, bool isAdd)
        {
            if (model.FormName.IsEmpty())
            {
                return ApiResult.RetErr(nameof(model.FormName), "窗体名称不能为空");
            }
            if (model.TextBoxName.IsEmpty())
            {
                return ApiResult.RetErr(nameof(model.TextBoxName), "控件名称不能为空");
            }
            if (model.ReturnTextField.IsEmpty())
            {
                return ApiResult.RetErr(nameof(model.ReturnTextField), "文本返回内容不能为空");
            }
            if (isAdd) model.Id = GuidHelper.NewId();
            if (DB.QueryExist<Sys_GridCombox>(s => s.FormName == model.FormName && s.TextBoxName == model.TextBoxName && s.Id != model.Id))
            {
                return ApiResult.RetErr(nameof(model.FormName), "窗体控件重复");
            }

            return ApiResult.RetOK("", "保存成功");
        }

        public ApiResult<string> AddMain(Sys_GridCombox model)
        {
            var ret = VerifyMainModel(model, true);
            if (ret)
            {
                DB.Insert(model);
            }
            return ret;
        }

        public ApiResult<string> EditMain(Sys_GridCombox model)
        {
            var ret = VerifyMainModel(model, false);
            if (ret)
            {
                DB.Update(model);
            }
            return ret;
        }

        public ApiResult DelMain(Sys_GridCombox model)
        {
            var r = DB.Delete<Sys_GridCombox>(s => s.Id == model.Id);
            DB.Delete<Sys_GridCombox_Column>(s => s.MainId == model.Id);
            if (r.RowCount == 1)
            {
                return ApiResult.RetOK("删除成功");
            }
            return ApiResult.RetErr("删除失败");
        }

        public DataTable GetColumnData(string mainId)
        {
            var ret = DB.Query<Sys_GridCombox_Column>(s => s.MainId == mainId);
            var dt = ret.ToDataTable();
            return dt;
        }

        ApiResult<string> VerifyColumnModel(Sys_GridCombox_Column model, bool isAdd)
        {
            if (model.ColumnCaption.IsEmpty())
            {
                return ApiResult.RetErr(nameof(model.ColumnCaption), "标题不能为空");
            }
            if (model.FiledName.IsEmpty())
            {
                return ApiResult.RetErr(nameof(model.FiledName), "字段名称不能为空");
            }
            if (isAdd) model.Id = GuidHelper.NewId();
            if (DB.QueryExist<Sys_GridCombox_Column>(s => s.MainId == model.MainId && s.FiledName == model.FiledName && s.Id != model.Id))
            {
                return ApiResult.RetErr(nameof(model.FiledName), "字段名称重复");
            }

            return ApiResult.RetOK("", "保存成功");
        }

        public ApiResult<string> AddColumn(Sys_GridCombox_Column model)
        {
            var ret = VerifyColumnModel(model, true);
            if (ret)
            {
                DB.Insert(model);
            }
            return ret;
        }

        public ApiResult<string> EditColumn(Sys_GridCombox_Column model)
        {
            var ret = VerifyColumnModel(model, false);
            if (ret)
            {
                DB.Update(model);
            }
            return ret;
        }

        public ApiResult DelColumn(Sys_GridCombox_Column model)
        {
            var r = DB.Delete<Sys_GridCombox_Column>(s => s.Id == model.Id);
            if (r.RowCount == 1)
            {
                return ApiResult.RetOK("删除成功");
            }
            return ApiResult.RetErr("删除失败");
        }

        #endregion
    }

}
